/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.device.ud;

import cn.net.greenet.common.db_service.BaseDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Ud 设备管理类
 *
 * @author ShiYun,2014/12/18
 */
public class UddeviceManageService {

    public static final String QUERY = "SELECT * FROM `shj_db`.`uddevice` WHERE ip=? AND port=?";
    public static final String QUERYALL = "SELECT * FROM `shj_db`.`uddevice`";
    public static final String INSERT = "INSERT INTO `shj_db`.`uddevice` VALUES (?,?,?,?,?,?,?,?)";
    public static final String UPDATE = "UPDATE `shj_db`.`uddevice` SET `ip` = ?,  `port` = ?, `isRun` = ?,`desc` = ?,`resv1` = ?,`resv2` = ?,`resv3` = ?,`resv4` = ? WHERE `ip` = ? AND `port` = ?;";
    public static final String DELETE = "DELETE FROM `shj_db`.`uddevice` WHERE `ip` = ? AND `port` = ?";
    private BaseDao dao = null;

    private void init() {
        dao = new BaseDao();
    }

    private void close() {
        if (dao != null) {
            dao.close();
        }
    }

    public boolean insert(String ip, String port, String isRun, String desc, String resv1, String resv2, String resv3, String resv4) {
        boolean flag = false;
        init();
        Object[] s = {ip, port, isRun, desc, resv1, resv2, resv3, resv4};
        int num = dao.executeUpdate(INSERT, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public boolean delete(String ip, String port) {
        boolean flag = false;
        init();
        Object[] s = {ip, port};
        int num = dao.executeUpdate(DELETE, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public boolean update(String old_ip, String old_port, String ip, String port, String isRun, String desc, String resv1, String resv2, String resv3, String resv4) {
        boolean flag = false;
        init();
        Object[] s = {ip, port, isRun, desc, resv1, resv2, resv3, resv4, old_ip, old_port};
        int num = dao.executeUpdate(UPDATE, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public JSONArray findBYID(String ip, String port) {
        JSONArray one = new JSONArray();
        try {
            init();
            Object[] s = {ip, port};
            ResultSet rs = dao.executeSQL(QUERY, s);
            if (rs.next()) {
                String ip1 = rs.getString(1);
                int port1 = rs.getInt(2);
                int isRun = rs.getInt(3);
                String desc = rs.getString(4);
//                String resv1 = rs.getString(5);
//                String resv2 = rs.getString(6);
//                String resv3 = rs.getString(7);
//                String resv4 = rs.getString(8);
                one.add(ip1);
                one.add(port1);
                one.add(isRun);
                one.add(desc);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UddeviceManageService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            close();
        }
        return one;
    }

    public JSONArray findAll() {
        init();
        JSONArray arr = new JSONArray();
        try {
            String[] s = {};
            ResultSet rs = dao.executeSQL(QUERYALL, s);
            while (rs.next()) {
//                JSONArray one = new JSONArray();
                String ip = rs.getString(1);
//                one.add(ip);
                int port = rs.getInt(2);
//                one.add(port);
                int isRun = rs.getInt(3);
//                one.add(isRun);
                String desc = rs.getString(4);
//                one.add(desc);
//                arr.add(one);
                JSONObject jsObj = new JSONObject();
                jsObj.put("ip", ip);
                jsObj.put("port", port);
                jsObj.put("isRun", isRun);
                jsObj.put("desc", desc);
                arr.add(jsObj);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UddeviceManageService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            close();
        }
        return arr;
    }

    public static void main(String[] args) {
        UddeviceManageService ud = new UddeviceManageService();
        System.out.println("start:" + ud.findAll());
        System.out.println(ud.findBYID("10.10.2.1", "21"));
        boolean f = ud.insert("10.10.1.3", "22", "0", "test", null, null, null, null);
        System.out.println("Add:" + f);
        System.out.println("end:" + ud.findAll());
        boolean flag = ud.delete("127.0.0.1", "22");
        System.out.println("Del:" + flag);
        boolean flag1 = ud.update("127.0.0.1", "23", "127.0.0.1", "24", "0", "test", null, null, null, null);
        System.out.println("update:" + flag1);
    }
}
